package com.platform.api.bzproject.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import com.platform.api.bzproject.model.BzTalent;
import com.platform.api.bzproject.service.IBzTalentService;
import com.platform.api.bzproject.service.SmsLogService;

import com.platform.api.bzproject.vo.TalentVo;
import com.platform.api.common.component.ImageCompressComponent;
import com.platform.common.core.Result;
import com.platform.common.core.ResultCode;
import com.platform.common.core.oss.AttachFileService;
import com.platform.common.core.oss.OssProcess;
import com.platform.common.enums.SmsType;
import com.platform.common.exception.PlatformBindException;


import com.platform.security.util.SecurityUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 达人注册表 前端控制器
 * </p>
 *
 * @author guoshao
 * @since 2021-10-
 * 源码联系微信：2549789059
 */
@RestController
@RequestMapping("{appid}/talent")
public class BzTalentController {

    @Autowired
    private IBzTalentService iBzTalentService;

    @Autowired
    private SmsLogService smsLogService;

    @Autowired
    private AttachFileService attachFileService;

    @Autowired
    private ImageCompressComponent imageCompressComponent;


    @GetMapping("checkWord")
    public Result checkWord(String word){
        int count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("word", word));
        if(count > 0){
            return Result.failure("口令不可用");
        }
        return Result.success("口令可用");
    }

    /**
     * 达人列表
     * @param talentVo
     * @param offset
     * @return
     */
    @GetMapping("/list")
    private Result getBalanceRecord( TalentVo talentVo, Integer offset){

        PageInfo<TalentVo> talentVos = iBzTalentService.selectBzTalentList(talentVo, offset);

        return Result.success(talentVos);
    }

    /**
     * 我的邀请记录
     * @param appid
     * @return
     */
    @GetMapping("apprevoList")
    public Result apprevoList(Integer offset,Integer status){

        Map<String,Object> map = new HashMap<>(6);
        map.put("status",status);

        PageInfo<TalentVo> pageInfo = iBzTalentService.apprevoList(map, offset);
        return Result.success(pageInfo);
    }

    @GetMapping("/getTalentInfo")
    private Result getTalentInfo( TalentVo talentVo){

        PageInfo<TalentVo> talentVos = iBzTalentService.selectBzTalentList(talentVo, 0);

        if(talentVos.getList().size() > 0){
            return Result.success(talentVos.getList().get(0));
        }

        return Result.failure("无数据");
    }

    @GetMapping("/getTalentByWord")
    private Result getTalentByWord( TalentVo talentVo){
        TalentVo talent = iBzTalentService.selectByWord(talentVo);
        if(null != talent){
            return Result.success(talent);
        }

        return Result.failure("无数据");
    }


    @PostMapping("register")
    public Result register(BzTalent talent, @RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        talent.setWord(talent.getWord().replaceAll(" ",""));
        talent.setPhone(talent.getPhone().replaceAll(" ",""));

        if(StringUtils.isNotBlank(talent.getRecommenderWord())){
            talent.setRecommenderWord(talent.getRecommenderWord().replaceAll(" ",""));
        }

        int count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("word", talent.getWord()));
        if(count > 0){
            return Result.failure("口令不可用");
        }
        count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("phone", talent.getPhone()));
        if(count > 0){
            return Result.failure("手机号码已注册");
        }

        String userId = SecurityUtils.getUser().getUserId();

        count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("userid", userId));
        if(count > 0){
            return Result.failure("不可重复绑定");
        }

        String systemType = StringUtils.isNotBlank(request.getHeader("systemType")) ? request.getHeader("systemType") : "0";

//        try {
//            boolean validCode = smsLogService.checkValidCode(talent.getPhone(), talent.getCode(), SmsType.VALID);
//            if(!validCode){
//                return Result.failure("验证码无效");
//            }
//        }catch (Exception e){
//            return Result.failure("验证码校验过频繁，请稍后再试");
//        }


        String domain = attachFileService.getDomain();

        file = imageCompressComponent.imageCompres(file);
        String s = attachFileService.uploadFile(file,null);

        String s1 = domain+"/"+ s + OssProcess.getCompressTx(attachFileService.getOssType());

        try {
            talent.setAvatarUrl(s1);
            talent.setMiniType(systemType);
            iBzTalentService.adduserInfo(talent);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("注册失败");
        }
        return Result.success();
    }



    @PostMapping("registerNew")
    public Result registerNew(BzTalent talent, @RequestParam("file") MultipartFile file, HttpServletRequest request) throws IOException {

        talent.setWord(talent.getWord().replaceAll(" ",""));
        talent.setPhone(talent.getPhone().replaceAll(" ",""));
        if(StringUtils.isNotBlank(talent.getRecommenderWord())){
            talent.setRecommenderWord(talent.getRecommenderWord().replaceAll(" ",""));
        }

        int count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("word", talent.getWord()));
        if(count > 0){
            return Result.failure("口令不可用");
        }
        count = iBzTalentService.count(new QueryWrapper<BzTalent>().eq("phone", talent.getPhone()));
        if(count > 0){
            return Result.failure("手机号码已注册");
        }

        String domain = attachFileService.getDomain();

        file = imageCompressComponent.imageCompres(file);
        String s = attachFileService.uploadFile(file,null);
        System.out.println(s);
        String s1 = domain+"/"+ s + OssProcess.getCompressTx(attachFileService.getOssType());

        try {
            talent.setAvatarUrl(s1);
            iBzTalentService.adduserInfoNew(talent);
        }catch (Exception e){
            System.out.println(e.getMessage());
            return Result.failure("注册失败");
        }
        return Result.success();
    }

    @GetMapping("apprevo")
    public Result apprevo(Integer id,Integer status){

        BzTalent talent = iBzTalentService.getById(id);
        if (null == talent)
            return Result.failure("处理失败");

        talent.setStatus(status);
        iBzTalentService.updateById(talent);

        return Result.success();
    }

}
